package com.singfan.common.db;

import com.singfan.common.SingFanException;
import com.singfan.common.db.provider.*;

/**
 * SingFan数据库环境类
 * Author: HanSongHe
 * Date: 15-7-1 17:17
 */
public class DbContext {
    private static DbConfigProvider dbConfigProvider;
    private static ConnectionPoolProvider connectionPoolProvider;

    /**
     * 初始化SingFan数据库环境
     */
    public static void init(){
        try {
            dbConfigProvider = new XmlDbConfigProvider();
            dbConfigProvider.init();

            connectionPoolProvider = new C3p0ConnectionPoolProvider();
            connectionPoolProvider.init(dbConfigProvider.getDataBaseList());

            DbLogger.init();
        }
        catch (Exception ex){
            throw new SingFanException(String.format("DbContext.init() error: %s", ex.getMessage()), ex);
        }
    }

    /**
     * 获取数据库配置信息提供类
     * @return 数据库配置信息提供类
     */
    public static DbConfigProvider getDbConfigProvider() {
        return dbConfigProvider;
    }

    /**
     * 获取数据库连接池提供类
     * @return 数据库连接池提供类
     */
    public static ConnectionPoolProvider getConnectionPoolProvider() {
        return connectionPoolProvider;
    }
}
